/**
 * angular 的缓存方法，服务于promise
 */
angular.module('com.tianshu.app').factory('cache', ['$cacheFactory',
    function ($cacheFactory) {
        return {
            flash: $cacheFactory('flash', {
                capacity: 1
            }),
            short: $cacheFactory('short', {
                capacity: 5
            }),
            long: $cacheFactory('long', {
                capacity: 20
            }),
            list: $cacheFactory('list', {
                capacity: 100
            })
        };
    }
])
/**
 *  http请求公用方法
 */
angular.module('com.tianshu.app').factory('promise', ['$http', '$q', function($http, $q) {
	return {
		get: function(param, url, cacheId, cache) {
			var result, defer = $q.defer();

			result = cacheId && cache && cache.get(cacheId);
			if (result) {
				defer.resolve(result);
			} else {
				$http({
					method: 'get',
					url: url,
					data: param
				}).success(function(data, status, headers, config) {
					if (cacheId && cache) {
						cache.put(cacheId, data);
					}
					defer.resolve(data);
				}).error(function(data, status, headers, config) {
					defer.reject(data)
				});

			}
			return defer.promise;
		}
	}
}])